package com.dhcc.sds.standard.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.plugins.Page;
import com.dhcc.cdc.busi.entity.CdcDictionaryEntity;
import com.dhcc.cdc.busi.entity.CdcLabItemEntity;
import com.dhcc.cdc.busi.entity.CdcLabOrderEntity;
import com.dhcc.cdc.busi.entity.CdcOrdmastEntity;
import com.dhcc.cdc.sys.service.IDictionaryService;
import com.dhcc.core.framework.base.controller.BaseController;
import com.dhcc.core.framework.base.warpper.EmptyWarpper;
import com.dhcc.core.framework.result.R;
import com.dhcc.core.framework.result.page.PageFactory;
import com.dhcc.sdc.cdss.entity.SdcCdssItemMatchEntity;
import com.dhcc.sds.base.entity.SdsExpressEntity;
import com.dhcc.sds.standard.dto.CdcLabOrderDto;
import com.dhcc.sds.standard.dto.CdcLabitemDto;
import com.dhcc.sds.standard.dto.CdcOrdmastDto;
import com.dhcc.sds.standard.entity.SdsStandDicMatchEntity;
import com.dhcc.sds.standard.service.ISdsStandDicMatchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;
import java.util.Objects;

@Controller
@RequestMapping("${sys.backendPath}/sds/sdsstanddicmatch")
public class SdsStandDicMatchController extends BaseController {

    private String PREFIX = "/backend/sds/sdsstanddicmatch/";

    @Autowired
    private ISdsStandDicMatchService sdsStandDicMatchService;
    @Autowired
    private IDictionaryService dictionaryService;

    @RequestMapping("")
    public String index(Model model) {
        return PREFIX + "sdsstanddicmatch.html";
    }

    /**
     * @Description: 查询医嘱对照数据
     * @Title: selectOrderMatch
     * @return: Object
     * @author: zhangdc
     * @date:2023-04-18 10:09:15
     */
    @RequestMapping("selectOrderMatch")
    @ResponseBody
    public Object selectOrderMatch(@RequestParam Map<String, Object> params) {

        String currentPage = (String) params.get("page");
        String size = (String) params.get("rows");
        String compare = (String) params.get("compare");
        String matchType = (String) params.get("matchType");

        if (Objects.equals("match", compare)){
            Page<SdsStandDicMatchEntity> page = new Page<SdsStandDicMatchEntity>(Integer.parseInt(currentPage), Integer.parseInt(size));
            List<CdcOrdmastDto> result = sdsStandDicMatchService.pageMatchOrder(page,params);
            Page<CdcOrdmastDto> dtoPage = new Page<CdcOrdmastDto>(Integer.parseInt(currentPage), Integer.parseInt(size));
            BeanUtil.copyProperties(page,dtoPage);
            dtoPage.setRecords(result);
            return R.packForBT(dtoPage);
        }else {
            Page<CdcOrdmastEntity> page = new Page<CdcOrdmastEntity>(Integer.parseInt(currentPage), Integer.parseInt(size));
            List<CdcOrdmastDto> result = sdsStandDicMatchService.pageOrder(page,params);

            Page<CdcOrdmastDto> dtoPage = new Page<CdcOrdmastDto>(Integer.parseInt(currentPage), Integer.parseInt(size));
            BeanUtil.copyProperties(page,dtoPage);
            dtoPage.setRecords(result);
            return R.packForBT(dtoPage);
        }
    }

    @RequestMapping(value = "/compare")
    @ResponseBody
    public Object compare(@RequestParam Long dicId, @RequestParam String orderIDStr, @RequestParam Integer matchType) {

        this.sdsStandDicMatchService.compare(dicId, orderIDStr, matchType);

        return R.ok();
    }

    @RequestMapping(value = "/undo")
    @ResponseBody
    public Object undo(@RequestParam Long dicId, @RequestParam String orderIDStr, @RequestParam Integer matchType) {
        this.sdsStandDicMatchService.undo(dicId, orderIDStr, matchType);

        return R.ok();
    }

    /**
     * @Description: 查询检验对照数据
     * @Title: selectLisMatch
     * @return: Object
     * @author: zhangdc
     * @date:2023-04-23 10:09:15
     */
    @RequestMapping("selectLisMatch")
    @ResponseBody
    public Object selectLisMatch(@RequestParam Map<String, Object> params) {
        String currentPage = (String) params.get("page");
        String size = (String) params.get("rows");
        String compare = (String) params.get("compare");

        if (Objects.equals("match", compare)){
            Page<SdsStandDicMatchEntity> page = new Page<SdsStandDicMatchEntity>(Integer.parseInt(currentPage), Integer.parseInt(size));
            List<CdcLabOrderDto> result = sdsStandDicMatchService.pageMatchLisOrder(page,params);

            Page<CdcLabOrderDto> dtoPage = new Page<CdcLabOrderDto>(Integer.parseInt(currentPage), Integer.parseInt(size));
            BeanUtil.copyProperties(page,dtoPage);
            dtoPage.setRecords(result);
            return R.packForBT(dtoPage);
        }else {
            Page<CdcLabOrderEntity> page = new Page<CdcLabOrderEntity>(Integer.parseInt(currentPage), Integer.parseInt(size));
            List<CdcLabOrderDto> result = sdsStandDicMatchService.pageLisOrder(page,params);

            Page<CdcLabOrderDto> dtoPage = new Page<CdcLabOrderDto>(Integer.parseInt(currentPage), Integer.parseInt(size));
            BeanUtil.copyProperties(page,dtoPage);
            dtoPage.setRecords(result);
            return R.packForBT(dtoPage);
        }
    }

    /**
     * @Description: 查询检验项目对照数据
     * @Title: selectLisItemMatch
     * @return: Object
     * @author: zhangdc
     * @date:2023-04-23 10:09:15
     */
    @RequestMapping("selectLisItemMatch")
    @ResponseBody
    public Object selectLisItemMatch(@RequestParam Map<String, Object> params) {
        String currentPage = (String) params.get("page");
        String size = (String) params.get("rows");
        String compare = (String) params.get("compare");

        if (Objects.equals("match", compare)){
            Page<SdsStandDicMatchEntity> page = new Page<SdsStandDicMatchEntity>(Integer.parseInt(currentPage), Integer.parseInt(size));
            List<CdcLabitemDto> result = sdsStandDicMatchService.pageMatchLisItem(page,params);

            Page<CdcLabitemDto> dtoPage = new Page<CdcLabitemDto>(Integer.parseInt(currentPage), Integer.parseInt(size));
            BeanUtil.copyProperties(page,dtoPage);
            dtoPage.setRecords(result);
            return R.packForBT(dtoPage);
        }else {
            Page<CdcLabItemEntity> page = new Page<CdcLabItemEntity>(Integer.parseInt(currentPage), Integer.parseInt(size));
            List<CdcLabitemDto> result = sdsStandDicMatchService.pageLisItem(page,params);

            Page<CdcLabitemDto> dtoPage = new Page<CdcLabitemDto>(Integer.parseInt(currentPage), Integer.parseInt(size));
            BeanUtil.copyProperties(page,dtoPage);
            dtoPage.setRecords(result);
            return R.packForBT(dtoPage);
        }
    }

    /**
     * @Description: 查询病历子标题对照数据
     * @Title: selectOrderMatch
     * @return: Object
     * @author: zhangdc
     * @date:2023-04-23 10:09:15
     */
    @RequestMapping("selectEmrMatch")
    @ResponseBody
    public Object selectEmrMatch(@RequestParam Map<String, Object> params) {

        String currentPage = (String) params.get("page");
        String size = (String) params.get("rows");
        String compare = (String) params.get("compare");

        if (Objects.equals("match", compare)){
            Page<SdsStandDicMatchEntity> page = new Page<SdsStandDicMatchEntity>(Integer.parseInt(currentPage), Integer.parseInt(size));
            List<CdcOrdmastDto> result = sdsStandDicMatchService.pageMatchEmr(page,params);

            Page<CdcOrdmastDto> dtoPage = new Page<CdcOrdmastDto>(Integer.parseInt(currentPage), Integer.parseInt(size));
            BeanUtil.copyProperties(page,dtoPage);
            dtoPage.setRecords(result);
            return R.packForBT(dtoPage);
        }else {
            Page<CdcOrdmastDto> page = new Page<CdcOrdmastDto>(Integer.parseInt(currentPage), Integer.parseInt(size));
            List<CdcOrdmastDto> result = sdsStandDicMatchService.pageEmr(page,params);
            page.setRecords((List<CdcOrdmastDto>) new EmptyWarpper(result).warp());
            return R.packForBT(page);
        }
    }

    /**
     * 根据关键字自动匹配
     */
    @RequestMapping(value = "/autoCompare")
    @ResponseBody
    public Object autoCompare(@RequestParam Long dicId, @RequestParam String dicName, @RequestParam Integer matchType) {
        this.sdsStandDicMatchService.autoCompare(dicId, dicName, matchType);
        return R.ok();
    }

}
